from tortoise.models import Model
from tortoise import fields
from datetime import datetime


class MoveOrder(Model):
    """
    调拨订单模型
    """
    moveid = fields.CharField(max_length=18, pk=True, description="调拨单号")
    owner = fields.IntField(description="经办人")
    whoutid = fields.IntField(description="调出仓库")
    whinid = fields.IntField(description="调入仓库")
    orderamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="订单金额")
    whamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="入库金额")
    yjamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="已结款")
    wjamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="未结款")
    statu = fields.CharField(max_length=30, description="状态")
    shtime = fields.DatetimeField(null=True, description="单据时间")
    remark = fields.CharField(max_length=100, null=True, description="备注")
    del_flag = fields.CharField(max_length=1, default="0", description="删除标志(0代表存在 2代表删除)")
    create_by = fields.CharField(max_length=64, default="", description="创建者")
    create_time = fields.DatetimeField(null=True, description="创建时间")
    update_by = fields.CharField(max_length=64, default="", description="更新者")
    update_time = fields.DatetimeField(null=True, description="更新时间")

    class Meta:
        table = "wms_move"
        table_description = "调拨订单表"

    def __str__(self):
        return f"MoveOrder(moveid={self.moveid}, statu={self.statu})"

    @property
    def is_deleted(self) -> bool:
        """判断订单是否被删除"""
        return self.del_flag == "2"


class MoveItem(Model):
    """
    调拨订单商品项模型
    """
    itemid = fields.IntField(pk=True, description="调拨项")
    moveid = fields.CharField(max_length=18, null=True, description="调拨单号")
    orderid = fields.IntField(null=True, description="序号")
    pid = fields.IntField(null=True, description="商品")
    pname = fields.CharField(max_length=50, null=True, description="商品名称")
    unit = fields.CharField(max_length=20, null=True, description="单位")
    unitprice = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="单价")
    number = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="数量")
    orderamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="金额")
    whnumber = fields.DecimalField(max_digits=10, decimal_places=2, default=0, description="出库数量")
    whamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="出库金额")
    whid = fields.IntField(null=True, description="仓库")
    whname = fields.CharField(max_length=20, null=True, description="仓库")
    remark = fields.CharField(max_length=255, null=True, description="备注")
    del_flag = fields.CharField(max_length=1, default="0", description="删除标志(0代表存在 2代表删除)")
    create_by = fields.CharField(max_length=64, default="", description="创建者")
    create_time = fields.DatetimeField(null=True, description="创建时间")
    update_by = fields.CharField(max_length=64, default="", description="更新者")
    update_time = fields.DatetimeField(null=True, description="更新时间")

    class Meta:
        table = "wms_move_item"
        table_description = "调拨订单商品项表"

    def __str__(self):
        return f"MoveItem(itemid={self.itemid}, pid={self.pid}, number={self.number})"

    @property
    def is_deleted(self) -> bool:
        """判断商品项是否被删除"""
        return self.del_flag == "2"